// $Id: channel.h 84 2011-04-24 09:23:49Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2011 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef CHANNEL_H_
#define CHANNEL_H_

#include <sys/queue.h>
#include <sys/time.h>

#include "nio/channel.h"

#include "handler.h"

struct niosocket;

typedef void (*niochannel_close_cb)(struct niochannel *channel,
		struct niosocket *socket, void *cbarg);

struct niochannel {
	struct niobuffer_factory factory;
	void *factory_arg;
	struct niosocket *socket;
	struct niobuffer *buffer;
	struct niohandler_list handlers;
	struct niohandler_list replaced;
	struct niohandler_item *handler;
	enum niochannel_direction direction;
	void *message;
	int keep_alive;
	niochannel_cb cb;
	void *cbarg;
	niochannel_close_cb close_cb;
	void *close_cbarg;
	TAILQ_ENTRY(niochannel) entry;
};

TAILQ_HEAD(niochannel_list, niochannel);

int niochannel_open(struct niochannel *channel,
		struct niosocket *socket, struct timeval *timeout,
		niochannel_close_cb cb, void *cbarg);

int niochannel_close(struct niochannel *channel);

struct niohandler_item *niochannel_find(
		struct niochannel *channel, const char *name);

int niochannel_promote(struct niochannel *channel, struct niohandler_item *from,
		struct niohandler_item *to, void *message);

int niochannel_demote(struct niochannel *channel, struct niohandler_item *from,
		struct niohandler_item *to, void *message);

int niochannel_setup(struct niochannel *channel);

int niochannel_teardown(struct niochannel *channel);

#endif /* CHANNEL_H_ */
